﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace _4DJackPot
{
    public partial class CompanyGameSplit : Dock
    {
        public String game_id { get; set; }
        public String draw_date { get; set; }
        public String company_id { get; set; }
        public String company_name { get; set; }
        public String company_logo_url { get; set; }
        public int splitValue { get; set; }

        public CompanyGameSplit(String game_id, String company_id, String draw_date)
        {
            InitializeComponent();

            this.game_id = game_id;
            this.company_id = company_id;
            this.draw_date = draw_date;

            Company company = new Company(company_id);
            company_name = company.companyNameString;
            company_logo_url = company.companyLogoUrl;

            lblGameIDValue.Text = game_id;
            lblCompanyValue.Text = company_name;
            lblDrawDateValue.Text = draw_date;
            pbCompanyLogo.Image = (Bitmap)global::_4DJackPot.Properties.Resources.ResourceManager.GetObject(company_logo_url);

            splitValue = 0;
            getSplit();
            if (splitValue != 0)
                setSplit();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtLeft_TextChanged(object sender, EventArgs e)
        {
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            int leftNum = Int32.Parse(txtLeft.Text);
            int rightNum = Int32.Parse(txtRight.Text);

            if(leftNum <= 95 && rightNum >= 5)
            {
                leftNum += 5;
                rightNum -= 5;
            }

            txtLeft.Text = leftNum.ToString();
            txtRight.Text = rightNum.ToString();
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            int leftNum = Int32.Parse(txtLeft.Text);
            int rightNum = Int32.Parse(txtRight.Text);

            if (rightNum <= 95 && leftNum >= 5)
            {
                rightNum += 5;
                leftNum -= 5;
            }

            txtLeft.Text = leftNum.ToString();
            txtRight.Text = rightNum.ToString();
        }

        private void getSplit()
        {
            string selectQueryString =
                "select ratio from game_split where game_id = " + this.game_id + ";";
            Global.dBCmd.CommandText = selectQueryString;
            System.Data.OleDb.OleDbDataReader reader = Global.dBCmd.ExecuteReader();
            while (reader.Read())
            {
                this.splitValue = Int32.Parse(reader[0].ToString());
            }
            reader.Close();
        }

        private void setSplit()
        {
            int leftNum = 100-splitValue;
            int rightNum = splitValue;
            txtLeft.Text = leftNum.ToString();
            txtRight.Text = rightNum.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Global.dBCmd.CommandText = "DELETE FROM game_split WHERE game_id = " + game_id + ";";
            Global.dBCmd.ExecuteNonQuery();
            Global.dBCmd.CommandText = "INSERT INTO game_split(game_id, ratio) VALUES(" + game_id + ", " + txtRight.Text + "); ";
            if(Global.dBCmd.ExecuteNonQuery() == 1)
            {
                MessageBox.Show("Succeed to change ratio.");
            }
            else
            {
                MessageBox.Show("Failed to change ratio. Please try again");
            }
        }
    }
}
